package com.xixing.weixin.cmd;

import com.xixing.weixin.exception.ArgumentException;
import com.xixing.weixin.exception.WeixinExecuteException;
import com.xixing.weixin.domain.Account;
import com.xixing.weixin.domain.Weixin;
import com.xixing.weixin.response.Response;
import com.xixing.weixin.response.TextResponse;
import com.xixing.weixin.utils.GzhuLibraryUtils;
import com.xixing.weixin.utils.GzhuSchoolUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.ResourceBundle;

/**
 * Created with IntelliJ IDEA.
 * User: Apple
 * Date: 13-4-3
 * Time: 下午3:33
 * To change this template use File | Settings | File Templates.
 */
public class LoginExecutor extends AbstractExecutor {
    private String[] schSysAlias;
    private String[] libSysAlias;
    protected static ResourceBundle resourceBundle = ResourceBundle.getBundle("command");

    public LoginExecutor() {
        setSchSysAliasName();
        setLibSysAliasName();
    }

    public void setSchSysAliasName() {
        this.schSysAlias = AbstractExecutor.splitCommand(AbstractExecutor.normalCommand(resourceBundle.getString("LoginExecutor.schSysAlias")));
    }

    public void setLibSysAliasName() {
        this.libSysAlias = AbstractExecutor.splitCommand(AbstractExecutor.normalCommand(resourceBundle.getString("LoginExecutor.libSysAlias")));
    }

    public static class Arg {
        public String system;
        public String username;
        public String password;

        public Arg(String system) {
            this.system = system;
        }
    }

    @Override
    public Response help(Weixin weixin, Object args) {
        return super.help(weixin, args);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public Response execute(Weixin weixin, Object args) throws WeixinExecuteException {
        if (!(args instanceof Arg)) throw new ArgumentException();
        Arg arg = (Arg) args;
        if (StringUtils.isBlank(arg.system)) throw new ArgumentException();

        if (StringUtils.isBlank(arg.username) && StringUtils.isBlank(arg.password)) {
            return help(weixin, args);
        }

        if (StringUtils.isBlank(arg.username)) throw new ArgumentException();
        if (StringUtils.isBlank(arg.password)) throw new ArgumentException();
        if (ArrayUtils.contains(schSysAlias, arg.system)) { //是否登陆校务系统
            boolean success = GzhuSchoolUtils.validUser(arg.username, arg.password);
            if (success) {
                List<Account> resultList = Account.findAccountsByWeixinCodeAndAccountType(weixin.getFromUserName(), schSysAlias[0]).getResultList();
                if (resultList.size() > 0) {   //验证是否存在
                    Account account = Account.findAccount(resultList.get(0).getId());
                    account.setStudentCode(arg.username);
                    account.setStudentPassword(arg.password);
                    account.merge();
                } else {
                    //保存登陆信息
                    Account.persistByWeixin(weixin.getFromUserName(), schSysAlias[0], arg.username, arg.password);
                }
                return TextResponse.create(weixin, resourceBundle.getString("LoginExecutor.School.Success"));
            } else {
                return TextResponse.create(weixin, resourceBundle.getString("LoginExecutor.School.Failure"));
            }
        } else if (ArrayUtils.contains(libSysAlias, arg.system)) {//是否登陆图书馆系统
            boolean success = GzhuLibraryUtils.login(arg.username, arg.password);
            if (success) {
                //保存登陆信息
                return TextResponse.create(weixin, resourceBundle.getString("LoginExecutor.School.Success"));
            } else {
                return TextResponse.create(weixin, resourceBundle.getString("LoginExecutor.School.Failure"));
            }
        }
        throw new ArgumentException();
    }

    @Override
    public Object parseArgs(Weixin weixin, Object args) throws WeixinExecuteException {
        assertWeixinMsgType(weixin, Weixin.TEXT);
        //判断为空
        if (args == null) {
            Arg arg = new Arg("");
            String[] cmds = splitCommand(weixin.getContent());
            if (cmds.length == 4) {  // login sys usr pwd
                arg.system = cmds[1];
                arg.username = cmds[2];
                arg.password = cmds[3];
            } else if (cmds.length == 2) {    //login sys
                arg.system = cmds[1];
            } else {
                throw new ArgumentException();
            }
            return arg;
        }

        if (!(args instanceof Arg)) throw new ArgumentException();
        Arg arg = (Arg) args;

        if (StringUtils.isNotBlank(arg.system)) {

            String[] cmds = splitCommand(weixin.getContent());
            if (cmds.length != 2) throw new ArgumentException();
            // usr pwd
            arg.username = cmds[0];
            arg.password = cmds[1];
        } else {
            String[] cmds = splitCommand(weixin.getContent());
            if (cmds.length == 1) {
                // sys
                arg.system = cmds[0];
            } else if (cmds.length == 3) {
                // sys usr pwd
                arg.system = cmds[0];
                arg.username = cmds[1];
                arg.password = cmds[2];
            } else {
                throw new ArgumentException();
            }
        }
        return arg;
    }
}
